package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划业务层处理
 */
@Transactional
@Service
public class NursingPlanServiceImpl
        extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    //查询所有护理计划
    @Override
    public List<NursingPlan> listAll() {
        return nursingPlanMapper.listAll();
    }

    //查询护理计划列表
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    //获取护理计划详细信息
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        //根据ID查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        //创建护理计划视图对象
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        //将护理计划的属性拷贝到视图对象中
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        //根据护理计划ID查询护理计划和护理项目的关系
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        //将查询到的护理计划和护理项目的关系设置到护理计划视图对象中
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    //新增护理计划
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        //创建护理计划对象
        NursingPlan nursingPlan = new NursingPlan();
        //将护理计划DTO中的属性拷贝到护理计划对象中（属性名称和类型一致，就可以进行拷贝）
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        //设置护理计划的创建时间为当前日期时间
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        //调用Mapper方法插入护理计划记录到数据库
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        //批量保存护理项目计划关系
        //这里解释为什么需要批量保存：因为它关联了多个护理项目，每个项目都需要单独的记录
        int count = nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(), nursingPlan.getId());
        //根据插入结果返回状态码：0表示失败，1表示成功
        return count == 0 ? 0 : 1;
    }

    //修改护理计划
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        //属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        //判断nursingPlanDto中的项目列表是否为空，如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
        if (nursingPlanDto.getProjectPlans() != null && nursingPlanDto.getProjectPlans().size() > 0) {
            //删除护理计划与护理项目的关系
            nursingProjectPlanMapper.deleteByPlanId(nursingPlanDto.getId());
            //批量添加护理计划与护理项目的关系
            nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(), nursingPlanDto.getId());
        }
        //别管项目列表是否为空，都要修改护理计划
        return nursingPlanMapper.updateNursingPlan(nursingPlan);
    }

    //删除护理计划信息
    @Override
    public int deleteNursingPlanById(Long id) {
        try {
            //删除护理计划与护理项目的关系
            nursingProjectPlanMapper.deleteByPlanId(id);
            return nursingPlanMapper.deleteNursingPlanById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}